using System;
using System.Linq.Expressions;
using ExtendedMvc.Elements;

namespace ExtendedMvc {
	public static class FormSelectionExtensions {
		public static CheckBoxList<T> CheckBoxList<T>(this ModelViewForm<T> form, string memberName) where T : class {
			return new CheckBoxList<T>(form.Helper, memberName).Select(form.Helper.ViewContext.ViewData.Eval(memberName));
		}

		public static CheckBoxList<T> CheckBoxList<T>(this ModelViewForm<T> form, Expression<Func<T, object>> expression) where T : class {
			return new CheckBoxList<T>(form.Helper, expression.GetNameFor(form.Helper.ViewDataContainer))
				.Select(expression.GetValueFrom(form.Helper.ViewDataContainer.Model));
		}

		public static RadioButtonSet<T> RadioButtonSet<T>(this ModelViewForm<T> form, string memberName) where T : class {
			return new RadioButtonSet<T>(form.Helper, memberName).Select(form.Helper.ViewContext.ViewData.Eval(memberName));
		}

		public static RadioButtonSet<T> RadioButtonSet<T>(this ModelViewForm<T> form, Expression<Func<T, object>> expression) where T : class {
			return new RadioButtonSet<T>(form.Helper, expression.GetNameFor(form.Helper.ViewDataContainer))
				.Select(expression.GetValueFrom(form.Helper.ViewDataContainer.Model));
		}

		public static ComboBox<T> ComboBox<T>(this ModelViewForm<T> form, string memberName) where T : class {
			return new ComboBox<T>(form.Helper, memberName).Select(form.Helper.ViewContext.ViewData.Eval(memberName));
		}

		public static ComboBox<T> ComboBox<T>(this ModelViewForm<T> form, Expression<Func<T, object>> expression) where T : class {
			return new ComboBox<T>(form.Helper, expression.GetNameFor(form.Helper.ViewDataContainer))
				.Select(expression.GetValueFrom(form.Helper.ViewDataContainer.Model));
		}

		public static ListBox<T> ListBox<T>(this ModelViewForm<T> form, string memberName) where T : class {
			return new ListBox<T>(form.Helper, memberName).Select(form.Helper.ViewContext.ViewData.Eval(memberName));
		}

		public static ListBox<T> ListBox<T>(this ModelViewForm<T> form, Expression<Func<T, object>> expression) where T : class {
			return new ListBox<T>(form.Helper, expression.GetNameFor(form.Helper.ViewDataContainer))
				.Select(expression.GetValueFrom(form.Helper.ViewDataContainer.Model));
		}
	}
}
